package com.liuyandog;



import com.liuyandog.DBserver.LoginServer;
import com.liuyandog.pojo.Administrator;
import com.liuyandog.pojo.Person;
import com.liuyandog.pojo.User;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;


public class LoginUI extends JFrame{
		public LoginUI() {
			super();


			setSize(600,400);
			setLocationRelativeTo(null);
			//setResizable(false);
			Container con=getContentPane();



			//标题
			JTextField idfield=new JTextField("1812336911",20);
			JPanel idFieldPanel=new JPanel();
			JLabel idFieldLabel=new JLabel("账号:");
			idFieldPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
			idFieldPanel.setSize(300,50);
			idFieldPanel.setLocation(150,100);
			idFieldPanel.add(idFieldLabel);
			idFieldPanel.add(idfield);
			con.add(idFieldPanel);
			//账号输入框
			JPasswordField passwordField=new JPasswordField("1234",30);
			passwordField.setSize(185,20);
			passwordField.setLocation(224,150);
			JPanel passFieldPanel=new JPanel();
			JLabel passFieldLabel=new JLabel("密码:");
			passFieldPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
			passFieldPanel.setSize(200,20);
			passFieldPanel.setLocation(105,150);
			passFieldPanel.add(passFieldLabel);
			passFieldPanel.add(passwordField);
			con.add(passFieldPanel);
			//密码



			//con.add(idfield);
			con.add(passwordField);
			JPanel title=new JPanel();
			title.setLayout(new FlowLayout());
			title.setBounds(20,0,600,50);
			JLabel jl=new JLabel();
			jl.setText("图书管理系统");
			jl.setFont(new Font("等线",Font.PLAIN+Font.CENTER_BASELINE,45));
			jl.setBounds(200,80,300,50);
			title.add(jl);//标题的容器,做到居中
			//标题





			//按钮组（登录，退出）
			JPanel buttonPanel=new JPanel();//按钮组面板
			buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
			JButton[] jButtons = new JButton[2];
			jButtons[0]=new JButton(" 登录");
			jButtons[1]=new JButton(" 退出 ");
			JFrame j=this;//本身
			jButtons[0].addActionListener(new ActionListener() {//登录按钮
				@Override
				//@liuquan 这里添加主窗口，将JFrame更换
				public void actionPerformed(ActionEvent e) {
					Person person = new LoginServer(idfield.getText(),new String(passwordField.getPassword())).Check();
					if(person!=null){

						if(person.level==0){
							JFrame registe=new UserMain((User)person);//新注册窗口,根据账号密码生成窗口

							j.setEnabled(false);
							j.setVisible(false);//设置为不可编辑不可见;
							registe.setVisible(true);
							registe.addWindowListener(new WindowAdapter() {//窗口事件
								@Override
								public void windowClosed(WindowEvent e) {//窗口关闭事件监听
									super.windowClosed(e);
									j.setEnabled(true);
									j.setVisible(true);//设置本身为可见;
								}
							});
						}
						else {
							JFrame registe=new AdministratorMainUI("管理员界面",(Administrator) person);//新注册窗口,根据账号密码生成窗口

							j.setEnabled(false);
							j.setVisible(false);//设置为不可编辑不可见;
							registe.setVisible(true);
							registe.addWindowListener(new WindowAdapter() {//窗口事件
								@Override
								public void windowClosed(WindowEvent e) {//窗口关闭事件监听
									super.windowClosed(e);
									j.setEnabled(true);
									j.setVisible(true);//设置本身为可见;
								}
							});

						}
					}else {
						JOptionPane.showMessageDialog(null, "账号或密码错误", "警告", JOptionPane.WARNING_MESSAGE);
					}
				}
			});
			jButtons[1].addActionListener(new ActionListener() {//退出监听事件
				@Override
				public void actionPerformed(ActionEvent e) {
					System.exit(0);
				}
			});
			for(JButton i:jButtons) {
				buttonPanel.add(new JLabel("        "));
				buttonPanel.add(i);
				buttonPanel.add(new JLabel("        "));
			}
			buttonPanel.setBounds(0,200,600,50);
			con.add(buttonPanel);
			con.add(title);
			//按钮设置

			setDefaultCloseOperation(EXIT_ON_CLOSE);
			setDefaultCloseOperation(EXIT_ON_CLOSE);
		}
		public static void main(String[] args) {
//			File f=new File("src\\WritingBoard\\data");
//			if(!f.exists())
//				f.mkdirs();
			LoginUI jf=new LoginUI();
			jf.setVisible(true);
		}
}
